package cn.my.springHDFS.web;

import cn.my.springHDFS.service.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Leo on 2017/7/3.
 * 用户控制层
 */
@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    //Logger方法
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @ApiOperation(value = "跳转到注册页面", httpMethod = "GET", notes = "跳转到注册页面")
    @RequestMapping(value = "/userRegister")
    public String goRegister(HttpServletRequest httpServletRequest){
        return "userRegister";
    }

    @ApiOperation(value = "注册方法", httpMethod = "POST", notes = "注册方法")
    @RequestMapping(value = "/user_register")
    @ResponseBody
    public boolean register(@RequestParam String username, @RequestParam String email, @RequestParam String password, @RequestParam String password_1,HttpServletRequest httpServletRequest) {
        System.out.println("Username: " + username + " " + "Email: " + email + "Password: " + password + "Password_1:" + password_1);
        return password.equals(password_1) && userService.addUser(username, email, password);
    }

    @ApiOperation(value = "验证用户是否存在", httpMethod = "POST", notes = "验证用户是否存在")
    @RequestMapping(value = "/validateUser")
    @ResponseBody
    public boolean validateUser(@RequestParam String account, HttpServletRequest httpServletRequest){
        return userService.validateUser(account);
    }

    @ApiOperation(value = "验证用户邮箱是否存在", httpMethod = "POST", notes = "验证用户邮箱是否存在")
    @RequestMapping(value = "/validateEmail")
    @ResponseBody
    public boolean validateEmail(@RequestParam String emailAddr, HttpServletRequest httpServletRequest){
        return userService.validateEmail(emailAddr);
    }

}
